package api

import (
	"context"
	"sort"
	"time"

	"dev.ixmxm.top/rms/managers/robotmanager"
	"dev.ixmxm.top/rms/model"
	"dev.ixmxm.top/rms/utils/log"

	"dev.ixmxm.top/rms/codes"

	"go.mongodb.org/mongo-driver/bson/primitive"
)

// @Summary 机器人分组添加操作
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body model.RobotGroup true "机器人分组"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 机器人
// @Router /robot_group/add [post]
func (hg *HttpGateway) AddRobotGroup(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*addRobotGroupParams)
	p.CreatedTime = time.Now()
	p.Id = primitive.NewObjectID()
	return nil, robotmanager.AddRobotGroup(ctx, &p.RobotGroup)
}

// @Summary 机器人分组跟新操作
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body model.RobotGroup true "机器人分组"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 机器人
// @Router /robot_group/update [post]
func (hg *HttpGateway) UpdateRobotGroup(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*updateRobotGroupParams)
	pID, err := primitive.ObjectIDFromHex(p.ID)
	if err != nil {
		return nil, codes.ErrInvalidParams.Wrap(err)
	}

	robotGroup := model.RobotGroup{
		Id:         pID,
		Name:       p.Name,
		MapList:    p.MapList,
		UpdateTime: time.Now(),
	}
	log.Errorf("get form map list %s", p.MapList)

	err = robotmanager.UpdateRobotGroup(ctx, &robotGroup)
	if err != nil {
		return nil, codes.ErrDatabase.Wrap(err)
	}

	return nil, nil
}

// @Summary 机器人分组获取操作
// @Description
// @Accept application/json
// @Produce application/json
// @Param id query string true "机器人分组ID"
// @Success 0 {object} model.RobotGroup "返回数据格式"
// @Tags 机器人
// @Router /robot_group/get [get]
func (hg *HttpGateway) GetRobotGroup(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*getRobotGroupParams)

	group, isOk := robotmanager.GetRobotGroup(p.ID)
	if !isOk {
		return nil, nil
	}
	return group, nil
}

// @Summary 获取分组下的机器人
// @Description
// @Accept application/json
// @Produce application/json
// @Param id query string true "机器人分组ID"
// @Success 0 {object} ResponseExampleList "返回数据格式"
// @Success 1 {object} []model.RobotAccount "data数据：机器人集合"
// @Tags 机器人
// @Router /robot_group/robots [get]
func (hg *HttpGateway) GetRobotGroupRobots(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*getGroupRobotsParams)
	group, err := robotmanager.GetAccountFromDBByGroup(ctx, p.ID)
	return group, err
}

// @Summary 机器人分组列表
// @Description
// @Accept application/json
// @Produce application/json
// @Success 0 {object} []model.RobotGroup "返回数据格式"
// @Tags 机器人
// @Router /robot_group/list [get]
func (hg *HttpGateway) GetAllRobotGroups(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	groups := robotmanager.GetAllRobotGroup()
	sort.SliceStable(groups, func(i, j int) bool {
		return groups[i].CreatedTime.Unix() < groups[j].CreatedTime.Unix()
	})
	return groups, nil
}

// @Summary 机器人分组删除操作
// @Description
// @Accept application/json
// @Produce application/json
// @Param id query string true "机器人分组ID"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 机器人
// @Router /robot_group/delete [delete]
func (hg *HttpGateway) DeleteRobotGroup(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*getRobotGroupParams)
	_, err := robotmanager.DeleteRobotGroup(ctx, p.ID)
	if err != nil {
		return nil, err
	}
	return nil, nil
}
